// pages/mine/mine/mine.js
const tools = require('../../../utils/tool')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    callSrc:'',   //联系方式图片路径
    popupShow:false,
    current: 0,
    bg: '',
    username: '--', //用户名
    MineInfo: {},
    lists: [], //收藏列表
    next_page_url: '',
    total: 0, //总页数
    page: 0, //当前页数
    articleid: false, //点赞列表的文章id 字段名为article_id 其余为id
    follow_num: '--',
    fans_num: '--',
    click_num: '--',
    level: 0,
    position_name: '',
    page:0
  },
  callPhone(){
    this.setData({
      popupShow: true,
      callSrc:'https://sch.antaiyi.com/kfh.jpg'
    });
  },
  
  callGzh(){
    this.setData({
      popupShow: true,
      callSrc:'https://sch.antaiyi.com/gzh.jpg'
    });
  },
  onClose() {
    this.setData({
      popupShow: false
    });
  },
   
  showPopup() {
    this.setData({ show: true });
  },

  

  onSelect(event) {
    console.log(event.detail);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onShow() {
     if(wx.getStorageSync('token')){
       this.setData({
         isLogin:true
       })
       console.log(1112233)
       this.getMineInfo().then(res=>{
        console.log(res)
      })
      console.log(this.data.current)
      if(this.data.current==0){
          this.getlists('/aty/applet/a/collect')
      }else{
          this.getlists('/aty/applet/a/praise')
      }
     }else{
      this.setData({
        isLogin:false
      })
     }
  },

  // 获取个人中心
  getMineInfo() {
    // tools.showLoading()
    return new Promise((resolve, reject) => {
      let getInfo = tools.request('/aty/applet/a/auth/info', 'get', {}, wx.getStorageSync('token'))
      getInfo.then(res => {
        console.log(res.data)
          resolve(res)
          // wx.hideLoading()
          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();
          if (res.statusCode==200) {
            this.setData({
              MineInfo: res.data.data,
              username: res.data.nickname,
              headimg: res.data.avatarUrl,
              
            })
          } else {
            tools.showToast(res.data.error_msg, 'none')
          }
        })
        .catch(err => {
          tools.showToast(err.errMsg, 'none')
        })
    })

  },
  showPopup() {
    this.setData({
      show: true
    });
  },

  onCancel() {
    this.setData({
      show: false
    });
  },
  onConfirm(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    this.setData({
      show: false
    });
  },
  onChange(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
  },
  onChangeCheck(event) {
    this.setData({
      checked: event.detail,
    });
  },
  // 切换发布 待审核 收藏 点赞列表
  bindTab(e) {
    let index = e.currentTarget.dataset.index
    let bg = e.currentTarget.dataset.bg
    this.setData({
      current: index,
      activeLevel: (index - 2) * 285,
      bg: bg,
      lists: [],
      page:0
    })
    if (index == 2) {
      this.getlists('/aty/applet/a/collect')
    } else if (index == 3) {
      this.getlists('/aty/applet/a/praise')
    }  
  },
  // 获取收藏列表
  getlists(url) {
    // tools.showLoading()
    let getInfo = tools.request(url, 'get', {
     page:this.data.page,
     size:5,
    }, wx.getStorageSync('token'))
    getInfo.then(res => {
        console.log(res)
        // wx.hideLoading()
        let lists = [...this.data.lists, ...res.data.content]
        if (res.statusCode==200) {
          this.setData({
            lists,
            totalElements:res.data.totalElements,
            pate:++this.data.page
          })
        } else {
          tools.showToast(res.data.error_msg, 'error')
        }
      })
      .catch(err => {
        tools.showToast(err.errMsg, 'error')
        console.log(err)
      })
  },

  onReachBottom: function () {
    let current = this.data.current //当前标签
    if(!current){
      if(this.data.totalElements>this.data.lists.length){
        this.getlists('/aty/applet/a/collect')
      }
    }else{
      if(this.data.totalElements>this.data.lists.length){
        this.getlists('/aty/applet/a/praise')
      }
    }
   
  }
})